import { Behaviour, SerilizeField, propertyEditor_Number, RenderableBehaviour, GameObject, TextRenderer } from "./gameengine";
import { MonsterMarshalMachine, MarshalMonsterIdleState } from "./Marshal";
import { stringify } from "querystring";


export class MonsterBehaviour extends Behaviour {

    marshal: MonsterMarshalMachine = null;

    isChange = false;

    gameObject:GameObject;

    fightUI:GameObject = null;

    protected onStart() {

        const marshal = new MonsterMarshalMachine();

        this.marshal = marshal;

        marshal.changeState(new MarshalMonsterIdleState())

    }
    protected onUpdate(advancedTime: number) {
        if(this.isChange && this.hp > 0) {
            this.hp--;
            if(this.hp <= 0) {
                this.gameObject.parent.removeChild(this.gameObject);
                if(this.fightUI){
                    this.fightUI.parent.removeChild(this.fightUI)
                }
            }
        }
    }

    @SerilizeField(propertyEditor_Number)
    hp: number = 0;

    attack: number = 0;
    defense: number = 0;

}

export class FightUI extends Behaviour{

    monster = null;

    text = "";

    onStart() {
    }

    onUpdate() {
        // console.log(this.monster)
        this.displayObject.getScript(TextRenderer).text = `${this.monster.hp}`;
    }

    fight(monster:GameObject){
        this.monster = monster.getScript(MonsterBehaviour);
    }

}